<template>
  <div class="card-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <span class="search-label">车牌号码：</span>
      <el-input v-model="params.carNumber" clearable placeholder="请输入内容" class="search-main" />
      <span class="search-label">车主姓名：</span>
      <el-input v-model="params.personName" clearable placeholder="请输入内容" class="search-main" />
      <span class="search-label">状态：</span>
      <el-select v-model="params.cardStatus">
        <el-option v-for="item in cardStatusList" :key="item.value" :value="item.value" :label="item.name" />
      </el-select>
      <el-button type="primary" class="search-btn" @click="selFn">查询</el-button>
    </div>
    <!-- 新增删除操作区域 -->
    <div class="create-container">
      <el-button type="primary" @click="$router.push('/cardAdd')">添加月卡</el-button>
      <el-button @click="allDelFn">批量删除</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="cardList" @selection-change="selectionChangeFn">>
        <el-table-column type="selection" />
        <el-table-column type="index" label="序号" />
        <el-table-column prop="personName" label="车主名称" />
        <el-table-column prop="phoneNumber" label="联系方式" />
        <el-table-column prop="carNumber" label="车牌号码" />
        <el-table-column prop="carBrand" label="车辆品牌" />
        <el-table-column prop="totalEffectiveDate" label="剩余有效天数" />
        <el-table-column prop="cardStatus" label="状态" :formatter="formatterFn" />
        <el-table-column label="操作" fixed="right" width="180">
          <template #default="{row}">
            <el-button size="mini" type="text">续费</el-button>
            <el-button size="mini" type="text" @click="$router.push(`/editMonthCard?id=${row.id}`)">查看</el-button>
            <el-button size="mini" type="text" @click="$router.push(`/cardAdd?id=${row.id}`)">编辑</el-button>
            <el-button size="mini" type="text" @click="delFn(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <div class="page-container">
      <el-pagination layout="total, prev, pager, next" :total="total" :page-size="params.pageSize" @current-change="currentChangeFn" />
    </div>
  </div>
</template>

<script>
import { getCardListAPI, delCardAPI, deleteCardsAPI } from '@/apis/card'
export default {
  data() {
    return {
      cardList: [],
      params: {
        page: 1,
        pageSize: 20,
        carNumber: null,
        personName: null,
        cardStatus: null
      },
      total: 0,
      cardStatusList: [
        {
          name: '全部',
          value: null
        },
        {
          name: '可用',
          value: 0
        },
        {
          name: '已过期',
          value: 1
        }
      ],
      selDataRowList: []

    }
  },
  mounted() {
    this.getCardList()
  },
  methods: {
    async getCardList() {
      const res = await getCardListAPI(this.params)
      this.cardList = res.data.rows
      this.total = res.data.total
      // 方案一：先在源头处理数据，在应用到表格
      // 数组字典（状态数字->字符串文字）
      // 作用：把数据转换成要展示的文字
      // const statusKey = {
      //   0: '可用',
      //   1: '已过期'
      // }
      // res.data.rows.map(ele => {
      //   // obj.cardstatus 数字当做 key 去 statusKey 字典里换出对应的字符串文字
      //   ele.status = statusKey[ele.cardStatus]
      // })
    },
    formatterFn(row) {
      // 方案二：后处理
      // row 是当前要格式化某列数据对应行上的对象
      // console.log(row)
      const statusKey = {
        0: '可用',
        1: '已过期'
      }
      return statusKey[row.cardStatus]
    },
    currentChangeFn(nowPage) {
      // console.log(nowPage)
      this.params.page = nowPage
      this.getCardList()
    },
    selFn() {
      // 坑: 如果先点击分页到后续页面 唯一的 params 对象里参数 page 就被改变了带着查询条件，去查询(后台给你返回后续页码的这个数据，实际符合条件的只有 1 个)，返回的空列表
      this.params.page = 1 // 可能你要的条件的数据只有 1个 (页面给我回到重新开始)
      this.getCardList()
    },
    delFn(id) {
      this.$confirm('此操作将永久删除该月卡, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delCardAPI(id)
        this.$message.error(res.msg)
        this.getCardList()
      }).catch((error) => {
        console.log(error)
      })
    },
    allDelFn() {
      this.$confirm('此操作将永久删除选中的这些月卡, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        console.log(this.selDataRowList)
        const idsList = this.selDataRowList.map(ele => ele.id)
        const res = await deleteCardsAPI(idsList)
        this.$message.success(res.msg)
        this.getCardList()
      }).catch((error) => {
        console.log(error)
      })
    },
    selectionChangeFn(list) {
      this.selDataRowList = list
    }
  }

}
</script>

<style lang="scss" scoped>
.card-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;

  .search-main {
    width: 220px;
    margin-right: 10px;
  }

  .search-btn {
    margin-left: 20px;
  }
}

.create-container {
  margin: 10px 0px;
}

.page-container {
  padding: 4px 0px;
  text-align: right;
}

.form-container {
  padding: 0px 80px;
}
</style>
